<template>
  <div class="view-content">

    <el-form ref="ruleFormRef" :model="ruleForm" :rules="rules" label-position="top" label-width="120px" class="demo-ruleForm" :size="formSize" status-icon style="max-width: 460px">
      <el-form-item label="姓名" prop="name">
        <el-input v-model="ruleForm.name" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="联系方式" prop="contact">
        <el-input v-model="ruleForm.contact" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="建议/反馈内容" prop="notes">
        <el-input v-model="ruleForm.notes" placeholder="请输入" type="textarea" />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="submitForm(ruleFormRef)">
          提交
        </el-button>
        <el-button @click="resetForm(ruleFormRef)">清空</el-button>
      </el-form-item>
    </el-form>
    <!-- <div id="mapDiv" style="width:500px; height:400px"></div> -->
  </div>
</template>

<script lang="ts" setup>
import { reactive, ref } from 'vue'
import axios from "axios"
import type { FormInstance, FormRules } from 'element-plus'
import { useRouter } from "vue-router";
const router = useRouter()

interface RuleForm {
  name: string,
  contact: string,
  notes: string
}
// onMounted(()=>{
//   const map = new T.Map('mapDiv');
//   map.centerAndZoom(new T.LngLat(116.40769, 39.89945), 12);
//   console.log(map.aa)
// })
const formSize = ref('default')
const ruleFormRef = ref<FormInstance>()
const ruleForm = reactive<RuleForm>({
  name: '',
  contact: '',
  notes: '',
})

const rules = reactive<FormRules<RuleForm>>({
  notes: [
    { required: true, message: '请输入您要反馈的内容', trigger: 'blur' },
  ],
})

const submitForm = async (formEl: FormInstance | undefined) => {
  if (!formEl) return
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!', ruleForm)
      axios({
        url: 'https://formspree.io/f/mzblkqpq',
        method: 'post',
        data: ruleForm
      }).then((res: any) => {
        if (res.data.ok) {
          console.log('成功！需要跳转页面，后面写')
          router.push('/success');
        }
      })
    } else {
      console.log('error submit!', fields)
    }
  })
}

const resetForm = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.resetFields()
}

</script>
